#coding=utf-8

import os
import sys
import time
import traceback

PATH = os.path.realpath(os.path.dirname(__file__))


def build_web(web_api=''):

    print('start build web...')

    web_api = web_api or 'http://192.168.1.160:5000'

    # 构建web端
    work_dir = os.path.join(PATH, 'web')
    os.chdir(work_dir)

    # 更换api地址
    api_base_file = os.path.join(PATH, 'web', 'src', 'services', 'ApiBase.js')
    fread = open(api_base_file, 'r')
    content = fread.read()
    fread.close()

    content = content.replace('http://192.168.1.160:5000', web_api)
    fwrite = open(api_base_file, 'w')
    fwrite.write(content)
    fwrite.close()

    # 安装依赖
    print('install web modules...')
    cmd_cnpm_install = '/usr/local/bin/cnpm i'
    os.system(cmd_cnpm_install)

    # 构建
    print('build web...')
    cmd_npm_build = '/usr/local/bin/npm run build'
    os.system(cmd_npm_build)


def build_api(db_str='', port='5533', obj_srv='192.168.1.201'):

    print('start build api...')

    db_str = db_str or 'postgresql://dev:asdasd123@192.168.1.158:5432/gis_temp'

    work_dir = os.path.join(PATH, 'api')
    os.chdir(work_dir)

    # 安装依赖
    print('pip install -r requirement.txt')
    cmd_install_dependency = 'pip install -r requirement.txt'
    os.system(cmd_install_dependency)

    ini = 'gis-manage.ini'

    setting_file = os.path.join(PATH, 'api', 'conf', 'settings.py')
    fread = open(setting_file, 'r')
    content = fread.read()
    fread.close()
    # 关掉debug
    content = content.replace('DEBUG = True', 'DEBUG = False')
    # 数据库
    content = content.replace('postgresql://dev:asdasd123@192.168.1.158:5432/gis_temp', db_str)
    content = content.replace('192.168.1.201', obj_srv)
    fwrite = open(setting_file, 'w')
    fwrite.write(content)
    fwrite.close()

    # 更改ini端口
    ini_file = os.path.join(PATH, 'api', 'gis-manage.ini')
    fread = open(ini_file, 'r')
    content = fread.read()
    fread.close()
    content = content.replace('5533', port)
    fwrite = open(ini_file, 'w')
    fwrite.write(content)
    fwrite.close()

    print('killing uwsgi')
    cmd_kill_uwsgi = "ps -ef | grep %s |  awk '{print $2}' | xargs kill -9" % ini
    os.system(cmd_kill_uwsgi)
    time.sleep(2)

    print('starting uwsgi')
    cmd_start_uwsgi = 'uwsgi %s' % ini
    os.system(cmd_start_uwsgi)
    time.sleep(2)

    print('uwsgi status:')
    cmd_uwsgi_status = 'ps -ef | grep %s' % ini
    os.system(cmd_uwsgi_status)
    time.sleep(1)

    sync_lock_file = os.path.join(PATH, 'api', 'shell', 'sync.lock')
    if os.path.exists(sync_lock_file):
        os.remove(sync_lock_file)


if __name__ == '__main__':
    print(sys.argv)

    try:
        # web：构建web，api：构建api
        build_target = 'web'

        if len(sys.argv) > 2:
            build_target = sys.argv[1]

            if build_target == 'web':
                web_api = sys.argv[2]
                build_web(web_api)
                print('---------build web finished--------')

            if build_target == 'api':
                db_str = sys.argv[2]
                port = sys.argv[3]
                obj_srv = sys.argv[4]
                build_api(db_str, port, obj_srv)
                print('---------build api finished--------')
    except:
        print(traceback.format_exc())


